package lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionEvent {
    public static void main(String[] args) {
            Frame frame=new Frame();
           Button btn1=new Button("start");
           Button btn2=new Button("stop");

            //可以显示的定义触发会返回的命令，如果不显示定义则显示默认值
        //可以多个按钮只写一个监听类
           btn2.setActionCommand("button-stop");
           btn1.setActionCommand("button-start");

        //创建一个事件
           MyMonitor myMonitor=new MyMonitor();

           btn1.addActionListener(myMonitor);
           btn2.addActionListener(myMonitor);

           frame.add(btn1,BorderLayout.NORTH);
           frame.add(btn2,BorderLayout.SOUTH);
           frame.pack();
           frame.setVisible(true);
           WindowClose(frame);

    }

    private static void WindowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                    System.exit(0);
            }
        });
    }
}
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand() 获取按钮的信息
        System.out.println("按钮被点击了：msg>="+e.getActionCommand());

    }
}

